/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.fatec.ads.facade;

import br.edu.fatec.ads.bo.BusinessObjectIdioma;
import br.edu.fatec.ads.classes.Idioma;
import br.edu.fatec.ads.util.ComboBoxItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;

/**
 *
 * @author Ubirajara
 */
public class FacadeIdioma {

    private static FacadeIdioma singleton = null;
    private BusinessObjectIdioma regraNegocio = null;

    private FacadeIdioma() {
        regraNegocio = new BusinessObjectIdioma();
    }

    public static FacadeIdioma getInstance(){
        if(singleton == null){
            singleton = new FacadeIdioma();
        }

        return singleton;
    }

    public List<Idioma> recuperarTodosIdiomas()
        throws HibernateException{
        List<Idioma> idiomas = Collections.EMPTY_LIST;

        regraNegocio.beginTransaction();

        try{
            idiomas = regraNegocio.recuperarTodosIdiomas();

            regraNegocio.commitTransaction();
        }catch(HibernateException cause){
            cause.printStackTrace();

            regraNegocio.rollbackTransaction();
        }

        return idiomas;
    }

    public List<ComboBoxItem> recuperarIdiomas(){
        List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
        List<Idioma> idiomas = this.recuperarTodosIdiomas();

        for (Idioma idioma : idiomas) {
            items.add(new ComboBoxItem
                        (idioma.getCodigo(),
                            idioma.getDescricao()));
        }

        return items;
    }

}